package com.ezsoft.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import com.ezsoft.test.controller.AppRegVO;

public class DbUtil {

    private static Connection getConn() {
        Connection conn = null;
        String drivername = "oracle.jdbc.driver.OracleDriver";
        String dbURL = "jdbc:oracle:thin:@localhost:1521:pms";
        String username = "gujianhua";
        String password = "123456";

        try {
            Class.forName(drivername);
            conn = DriverManager.getConnection(dbURL, username, password);
            System.out.println("连接成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("连接失败！！");
        }

        return conn;
    }

    /**
     * @author
     * @param appRegVO
     * @return
     */
    private static int insert(AppRegVO appRegVO) {

        Connection connection = getConn();

        int i = 0;

        String sql = "insert into appreg values(?,?,?,?,?,?,?,?,?,?)";

        PreparedStatement pstmt;

        try {
            pstmt = connection.prepareStatement(sql);

            pstmt.setString(1, appRegVO.getAppid());
            pstmt.setString(2, appRegVO.getAppname());
            pstmt.setString(3, appRegVO.getAppcode());
            pstmt.setString(4, appRegVO.getApptoken());
            pstmt.setString(5, appRegVO.getDeleteflag());
            pstmt.setString(6, appRegVO.getCreatetime());
            pstmt.setString(7, appRegVO.getCreateuser());
            pstmt.setString(8, appRegVO.getUpdatetime());
            pstmt.setString(9, appRegVO.getUpdateuser());
            pstmt.setString(10, appRegVO.getRemark());
            i = pstmt.executeUpdate();
            pstmt.close();
            connection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return i;
    }

    private static int update(AppRegVO appRegVO) {
        Connection connection = getConn();
        int i = 0;
        String sql = "update appreg set appname=? where appid=? ";

        PreparedStatement pstmt;

        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, appRegVO.getAppname());
            pstmt.setString(2, appRegVO.getAppid());
            i = pstmt.executeUpdate();
            pstmt.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return i;

    }

    public static int delete(String appid) {
        Connection connect = getConn();
        int i = 0;
        String sql = "delete from appreg where appid=?";
        PreparedStatement stmt = null;
        try {
            stmt = connect.prepareStatement(sql);
            stmt.setString(1, appid);
            i = stmt.executeUpdate();
            stmt.close();
            connect.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

}
